package com.stylefeng.guns.modular.finance.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.modular.system.model.ChannelSuper;
import com.stylefeng.guns.modular.system.model.CostChannel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CostLoan;
import com.stylefeng.guns.modular.finance.service.ICostLoanService;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 贷款收入控制器
 *
 * @author fengshuonan
 * @Date 2018-08-26 15:57:37
 */
@Controller
@RequestMapping("/costLoan")
public class CostLoanController extends BaseController {

    private String PREFIX = "/finance/costLoan/";

    @Autowired
    private ICostLoanService costLoanService;

    /**
     * 跳转到贷款收入首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "costLoan.html";
    }

    /**
     * 跳转到添加贷款收入
     */
    @RequestMapping("/costLoan_add")
    public String costLoanAdd() {
        return PREFIX + "costLoan_add.html";
    }

    /**
     * 跳转到修改贷款收入
     */
    @RequestMapping("/costLoan_update/{costLoanId}")
    public String costLoanUpdate(@PathVariable Integer costLoanId, Model model) {
        CostLoan costLoan = costLoanService.selectById(costLoanId);
        model.addAttribute("item",costLoan);
        LogObjectHolder.me().set(costLoan);
        return PREFIX + "costLoan_edit.html";
    }

    /**
     * 获取贷款收入列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<CostLoan> costServiceEntityWrapper = new EntityWrapper<>();
        costServiceEntityWrapper.like("company", condition);
        List<Map<String, Object>> mapList = costLoanService.selectMaps(costServiceEntityWrapper);
        BigDecimal total = new BigDecimal(0.00);
        for (Map<String, Object> map: mapList) {
            total = total.add((BigDecimal) map.get("amount"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("amount", total);
        mapList.add(0, map);
        return mapList;
    }

    /**
     * 新增贷款收入
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CostLoan costLoan) {
        costLoan.setCreateTime(new Date());
        costLoanService.insert(costLoan);
        return SUCCESS_TIP;
    }

    /**
     * 删除贷款收入
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer costLoanId) {
        costLoanService.deleteById(costLoanId);
        return SUCCESS_TIP;
    }

    /**
     * 修改贷款收入
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CostLoan costLoan) {
        costLoanService.updateById(costLoan);
        return SUCCESS_TIP;
    }

    /**
     * 贷款收入详情
     */
    @RequestMapping(value = "/detail/{costLoanId}")
    @ResponseBody
    public Object detail(@PathVariable("costLoanId") Integer costLoanId) {
        return costLoanService.selectById(costLoanId);
    }

    /**
     * 查询主渠道列表
     * @param
     * @return
     */
    @RequestMapping(value = "/queryLoanCompanyList")
    @ResponseBody
    public Object queryChannelList() {
//        List<ChannelSuper> channelSupers = channelSuperService.selectList(null);
//        return channelSupers;
        return null;
    }
}
